﻿using ArticleandSiteData;
using ModelClassLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上提供

namespace SplitViewOptions
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class NavigatePage : Page
    {
        public NavigatePage()
        {
            this.InitializeComponent();
        }
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if(e.NavigationMode==NavigationMode.New)
            {
                try
                {
                    List<Info> allsite = await BmobWebsiteData.Getrecommendsites(App.Recommendsiteurl);
                    var s = allsite.OrderByDescending<Info, string>(adr3);
                    List<ItemInGroup2> Items = (from Item in s orderby Item.Contant group Item by Item.Contant into newItems select new ItemInGroup2 { Key = newItems.Key, ItemContant = newItems.ToList() }).ToList();
                    IOrderedEnumerable<ItemInGroup2>   ordersult= Items.OrderBy<ItemInGroup2, string>(adr);
                    this.itemcollectionSource.Source = ordersult;
                    //  outView.ItemsSource = itemcollectionSource.View.CollectionGroups.OrderBy<object, string>(adr2);
                    outView.ItemsSource = itemcollectionSource.View.CollectionGroups;
                    inView.ItemsSource = itemcollectionSource.View;
                  //  navigateviewer.ItemsSource = allsite;
                }
                catch (Exception r)
                {

                }
            }
        }
        Func<ItemInGroup2, string> adr = delegate (ItemInGroup2 s)
        {
            return s.Key;
        };
        Func<Info, string> adr3 = delegate (Info s)
        {
            return s.text;
        };
        Func<object, string> adr2 = delegate (object s)
        {
            object i = s;
            string srt = s.ToString();
            string os = s.GetType().ToString();
         
            Debug.WriteLine(s);
            return "";
        };
        private void Border_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Border obj = sender as Border;
            Info oneinfo = obj.DataContext as Info;
                      
            App.Currenturl = new Uri(oneinfo.dataurl, UriKind.RelativeOrAbsolute);          
            this.Frame.Navigate(typeof(HomePage));
          //  App.iswithoutpicmode = false;
         //   App.isdownloadstirngmode = false;
         
        }

        private void Grid_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Grid obj = sender as Grid;
            Info oneinfo = obj.DataContext as Info;

            App.Currenturl = new Uri(oneinfo.dataurl, UriKind.RelativeOrAbsolute);
            this.Frame.Navigate(typeof(HomePage));
        }
    }
    public class ItemInGroup2
    {
        public string Key { get; set; }
        public List<Info> ItemContant { get; set; }
    }

}
